#include <nxos.h>

NX_Error PowerBoxBuiltinSignal(int argc, char *argv[], char * env)
{
    NX_U32 pid = 0;
    NX_Signal signal = NX_SIGNAL_TERMINATE;
    NX_Size signalValue = 0;
    NX_Size signalTimes = 1;
    int i;
    NX_Error err;
    
    if (argc < 2)
    {
        NX_Printf("usage: signal pid [signal] [signal value] [signal count]\n"
                  "        default signal is terminate.\n        default signal value is 0.\n        default signal count is 1.\n");
        return NX_EINVAL;
    }
    else
    {
        pid = NX_StrToUL(argv[1], 0, 10);
        if (argc >= 3)
        {
            signal = NX_StrToUL(argv[2], 0, 10);
        }
        if (argc >= 4)
        {
            signalValue = NX_StrToUL(argv[3], 0, 10);
        }
        if (argc >= 5)
        {
            signalTimes = NX_StrToUL(argv[4], 0, 10);
            if (signalTimes < 1)
            {
                signalTimes = 1;
            }
            if (signalTimes > 1024)
            {
                signalTimes = 1024;
            }
        }
    }

    NX_Printf("signal %d to pid %d with value %p for %d times\n", signal, pid, signalValue, signalTimes);
    for (i = 0; i < signalTimes; i++)
    {
        err = NX_SignalSend(pid, signal, (void *)signalValue);
        NX_Printf("[%d] signal result: %s\n", i, NX_ErrorToString(err));
    }
    return NX_EOK;
}
